package com.jzy.bxb.model.mongo.dao;

import java.util.List;
import java.util.stream.Collectors;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.dao.BasicDAO;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzy.bxb.core.db.mongo.AbsMongoManager;
import com.jzy.bxb.model.mongo.entity.config.ConfigCity;
import com.mongodb.MongoClient;

/**
 * 省市区
 * 
 * @author JiangZhiYong
 *
 */
@Repository
public class ConfigCityDao extends BasicDAO<ConfigCity, String> {

	private static volatile ConfigCityDao configCityDao;
	
	@Autowired
	public ConfigCityDao(Datastore ds){
		super(ds);
	}

	public ConfigCityDao(AbsMongoManager mongoManager) {
		super(ConfigCity.class, mongoManager.getMongoClient(), mongoManager.getMorphia(),
				mongoManager.getMongoConfig().getDbName());
	}

	public ConfigCityDao(Class<ConfigCity> entityClass, MongoClient mongoClient, Morphia morphia, String dbName) {
		super(entityClass, mongoClient, morphia, dbName);
	}

	public static ConfigCityDao init(AbsMongoManager mongoManager) {
		if (configCityDao == null) {
			synchronized (UserDao.class) {
				if (configCityDao == null) {
					configCityDao = new ConfigCityDao(mongoManager);
				}
			}
		}
		return configCityDao;
	}
	
	public static ConfigCityDao getConfigCityDao() {
		return configCityDao;
	}
	
	public static void setConfigCityDao(ConfigCityDao configCityDao) {
		ConfigCityDao.configCityDao = configCityDao;
	}

	public static void saveConfigCity(ConfigCity configCity) {
		configCityDao.save(configCity);
	}

	public static ConfigCity getConfigCity(String city) {
		Query<ConfigCity> query = configCityDao.createQuery().filter("name", city);
		return query.get();
	}
	

	public static List<String> getCityNames() {
		List<String> list = configCityDao.createQuery().asList().stream().map(ConfigCity::getName)
				.collect(Collectors.toList());
		return list;
	}
}
